import 'package:flutter/material.dart';
import 'package:manual/theme/screen_modal_theme.dart';

class ScreenModal {
  static Future<bool?> showHalfScreenModal(
    BuildContext context, {
    required List<Widget> children,
    bool showHeadButtons = true, // 控制是否显示按钮
  }) {
    final theme = Theme.of(context);
    final screenModalTheme = theme.extension<ScreenModalTheme>()!;

    return showModalBottomSheet<bool>(
      context: context,
      isScrollControlled: true, // 支持内容滚动
      backgroundColor: Colors.transparent, // 透明背景，方便自定义样式
      builder: (BuildContext context) {
        return DraggableScrollableSheet(
          initialChildSize: 0.7, // 初始高度为 70% 屏幕
          minChildSize: 0.7,     // 最小高度为 70% 屏幕
          maxChildSize: 1.0,     // 最大高度为 100% 屏幕
          builder: (context, scrollController) {
            return Container(
              decoration: BoxDecoration(
                color: screenModalTheme.backgroundColor,
                borderRadius: const BorderRadius.vertical(
                  top: Radius.circular(16.0),
                ),
              ),
              child: Column(
                children: [
                  // 顶部指示器和按钮栏
                  Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 16.0 ),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      crossAxisAlignment  :CrossAxisAlignment.start,
                      children: [
                        // 如果 showHeadButtons 为 true，则显示关闭按钮，否则显示空容器占位
                        if (showHeadButtons)
                          TextButton(
                            style: ButtonStyle(
                              padding: WidgetStateProperty.all(
                                const EdgeInsets.symmetric(vertical: 0.0),
                              ),
                              minimumSize: WidgetStateProperty.all(
                                const Size(64, 24),
                              ),
                            ),
                            onPressed: () {
                              Navigator.pop(context, false); // 返回 false 并关闭
                            },
                            child: const Text(
                              '关闭',
                              style: TextStyle(color: Colors.red),
                            ),
                          )
                        else
                          const SizedBox(width: 64), // 占位，保持平衡
                          // 顶部拖拽指示器
                          Container(
                            width: 40.0,
                            height: 4.0,
                            margin: const EdgeInsets.symmetric(vertical: 8.0),
                            decoration: BoxDecoration(
                              color: Colors.grey[300],
                              borderRadius: BorderRadius.circular(2.0),
                            ),
                          ),
                        // 如果 showHeadButtons 为 true，则显示完成按钮，否则显示空容器占位
                        if (showHeadButtons)
                          TextButton(
                            style: ButtonStyle(
                              padding: WidgetStateProperty.all(
                                const EdgeInsets.symmetric(vertical: 0.0),
                              ),
                              minimumSize: WidgetStateProperty.all(
                                const Size(64, 24),
                              ),
                            ),
                            onPressed: () {
                              Navigator.pop(context, true); // 返回 true 并关闭
                            },
                            child: const Text(
                              '完成',
                              style: TextStyle(color: Colors.green),
                            ),
                          )
                        else
                          const SizedBox(width: 64), // 占位，保持平衡
                      ],
                    ),
                  ),
                  // 可滚动内容
                  Expanded(
                    child: SingleChildScrollView(
                      controller: scrollController, // 绑定滚动控制器
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: children, // 动态注入子组件
                      ),
                    ),
                  ),
                ],
              ),
            );
          },
        );
      },
    );
  }
}
